import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:umeng_common_sdk/umeng_common_sdk.dart';


class CGUmengAppAnalysis extends NavigatorObserver {
  @override
  void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
    if (route.settings.name != null) {
      CGAnalysis.onPageStart(route.settings.name);
    }
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
    if (route.settings.name != null) {
      CGAnalysis.onPageEnd(route.settings.name);
    }
  }
}

class CGAnalysis{

  static String umStartPage;

  static void onPageStart (String pageName)async {
    String page = CGAnalysis.pageName(pageName);
    if ( page != umStartPage &&(page.length > 0)){
      if (umStartPage != ''){
        await UmengCommonSdk.onPageEnd(umStartPage);
      }
      umStartPage = page;
      await UmengCommonSdk.onPageStart(page);
    }
  }


  static void onPageEnd (String pageName) async {

    String  page = CGAnalysis.pageName(pageName);
    if ( (page == umStartPage )&&(umStartPage != '')){
      await UmengCommonSdk.onPageEnd(umStartPage);
      umStartPage = '';
    }
  }

  static String pageName(String pageName){
    String name = pageName??'';
    if (name == '/') name = '';
    if (name == '/appHome') name = '';
    return name;
  }
}

Future<void> initUmengAnalytics() async {
  UmengCommonSdk.initCommon(
      "57b26cf567e58e5c740003dd", "57b26d9e67e58e3ca0003844", "Umeng"
  );
  // 手动采集页面信息
  UmengCommonSdk.setPageCollectionModeManual();
  print('Umeng initialized.');
}
